
public class Testutf8Encode {
	
	public static String decodeUTF8(String src) {
		char[] chars = null;
		int leng = 0;

		try {
			if (null != src && src != "") {
				src = src.trim();
				String[] unitArray = src.split("%");
				chars = new char[unitArray.length];

				for (int i = 1; i < unitArray.length;) {
					int data = 0;
					byte[] bytes = new byte[5];
					bytes[0] = hexStringToByte(unitArray[i]);
					int byteNum = getLeftCountOf1InByte(bytes[0]);

					for (int j = 1; j < byteNum; j++) {
						bytes[j] = (byte) (hexStringToByte(unitArray[i + j]) & 0x3F);
					}
					bytes[0] = maskFirstByte(bytes[0]);
					int byteCount = 0;
					for (int j = byteNum - 1; j >= 0; j--) {
						data |= bytes[j] << (6 * byteCount);
						byteCount++;
					}
					chars[leng++] = (char) data;
					i += byteNum;
				}

				return new String(chars, 0, leng);
			}
		} catch (Exception e) {

		}

		return "";
	}

	public static byte hexStringToByte(String hexStr) {
		hexStr = hexStr.toUpperCase();
		char high = hexStr.charAt(0);
		char low = hexStr.charAt(1);

		int highVal = 0;
		if (high >= '0' && high <= '9') {
			highVal = high - '0';
		} else {
			highVal = 10 + (high - 'A');
		}
		int lowVal = 0;
		if (low >= '0' && low <= '9') {
			lowVal = low - '0';
		} else {
			lowVal = 10 + (low - 'A');
		}
		return (byte) ((highVal << 4) | lowVal);
	}

	public static int getLeftCountOf1InByte(byte b) {
		int count = 0;
		int mask = 1 << 7;
		for (int i = 0; i < 8; i++) {
			if ((b & mask) == 0) {
				break;
			} else {
				count++;
				mask >>= 1;
			}
		}
		return count;
	}
	

	private static byte maskFirstByte(byte b) {
		int mask = 1 << 7;
		for (int i = 0; i < 8; i++) {
			if ((b & mask) == 0) {
				break;
			} else {
				b &= ~mask;
				mask >>= 1;
			}
		}
		return (byte) b;
	}
	
	
	
	
}

	